# coding = utf-8
from django.db.models import Q
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from wxchat.api.permissions import WeixinPermission

from office.models import Office
from .serializers import OfficeSerializer, OfficeInfoSerializer


class OfficeViewSet(ReadOnlyModelViewSet):
    authentication_classes = []
    permission_classes = ()
    pagination_class = None
    queryset = Office.objects.all()
    serializer_class = OfficeSerializer


class OfficeInfoViewSet(ModelViewSet):
    # authentication_classes = []
    permission_classes = ()
    pagination_class = None
    queryset = Office.objects.all()
    serializer_class = OfficeInfoSerializer

    def get_queryset(self):
        queryset = super().get_queryset()
        name = self.request.query_params.get("name")

        if name:
            queryset = queryset.filter(Q(name__contains=name) | Q(address__contains=name) | Q(telephone__contains=name))

        return queryset


class OfficeFirstViewSet(ListAPIView):
    authentication_classes = []
    permission_classes = ()
    pagination_class = None
    queryset = Office.objects.all()
    serializer_class = OfficeInfoSerializer

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset[0:1]





